gulp           = require 'gulp'
autoprefixer   = require 'gulp-autoprefixer'
runSequence    = require 'run-sequence'
cached         = require 'gulp-cached'
concat         = require 'gulp-concat'
remember       = require 'gulp-remember'
sourcemaps     = require 'gulp-sourcemaps'
sassStream     = require './sassStream.coffee'
coffeeStream   = require './coffeeStream.coffee'
uglify         = require 'gulp-uglify'
minifyCss      = require 'gulp-minify-css'
filter         = require('gulp-filter')
mainBowerFiles = require('main-bower-files')
rename         = require('gulp-rename')
del            = require 'del'
webserver      = require 'gulp-webserver'

config     =
                paths:
                    dest: './www'
                    styles: 'app/**/*.scss'
                    scripts: 'app/**/*.coffee'
                    assets: [
                        'app/assets/**'
                        'app/**/*.html'
                    ]
                    index: 'app/index.html'
                prefixConfig:
                    browsers: ['Android > 5%', 'Chrome > 5%', 'iOS > 5%']

gulp.task 'clean',(done) ->
    del('./www/**', done)

gulp.task 'styles', ->
    gulp.src(config.paths.styles)
    .pipe(sourcemaps.init())
    .pipe(cached('stylesCache'))
    .pipe sassStream()
    .pipe autoprefixer(config.prefixConfig)
    .pipe(remember('stylesRem'))
    .pipe(concat('app.css'))
    .pipe sourcemaps.write('.')
    .pipe(gulp.dest(config.paths.dest))

gulp.task 'index', ->

gulp.task 'defaultReal', -> # 测试机环境
    runSequence('clean', [
        'styles'
        'scripts'
        'bower'
        'assets'
        'index'
    ])
gulp.task 'dist', -> # dist环境
    runSequence('clean', [
        'styles-optimize'
        'scripts-optimize'
        'bower'
        'assets'
        'index'
    ])

gulp.task 'scripts', ->
    gulp.src(config.paths.scripts)
    .pipe(sourcemaps.init())
    .pipe(cached('coffeeCache'))
    .pipe(coffeeStream())
    .pipe(remember('coffeeRem'))
    .pipe(concat('app.js'))
    .pipe sourcemaps.write('.')
    .pipe(gulp.dest(config.paths.dest))

gulp.task 'assets', ->
    gulp.src config.paths.assets[0]
    .pipe gulp.dest(config.paths.dest)

    gulp.src config.paths.assets[1]
    .pipe gulp.dest(config.paths.dest)

gulp.task 'scripts-optimize', ->
    gulp.src(config.paths.scripts)
    .pipe(coffeeStream())
    .pipe(concat('app.js'))
    .pipe(uglify())
    .pipe(gulp.dest(config.paths.dest))

gulp.task 'styles-optimize', ->
    gulp.src(config.paths.styles)
    .pipe sassStream()
    .pipe autoprefixer(config.prefixConfig)
    .pipe(minifyCss())
    .pipe(concat('app.css'))
    .pipe(gulp.dest(config.paths.dest))

gulp.task 'bower', ->
    jsFilter   = filter('*.js')
    cssFilter  = filter('*.css')
    fontFilter = filter(['*.eot', '*.svg', '*.woff', '*.ttf'])
    gulp.src(mainBowerFiles())
    .pipe(jsFilter)
    .pipe(concat('vendor.js'))
    .pipe(uglify())
    .pipe(jsFilter.restore())

    .pipe(cssFilter)
    .pipe(concat('vendor.css'))
    .pipe(minifyCss())
    .pipe(cssFilter.restore())

    .pipe(fontFilter)
    .pipe(rename((path) -> # 对于字体，放到fonts目录下
                path.dirname = '/fonts'
            ))
    .pipe(fontFilter.restore())

    .pipe(gulp.dest('./www'))

gulp.task 'watchReal', ['defaultReal'], -> # 测试机环境
    gulp.watch(config.paths.styles, ['styles'])
    .on 'change', (e) ->
        if e.type == 'deleted'
            delete cached.caches.stylesCache[e.path]
            remember.forget('stylesRem', e.path)

    gulp.watch(config.paths.scripts, ->
        runSequence('scripts')
    )
    .on 'change', (e) ->
        if e.type == 'deleted'
            delete cached.caches.coffeeCache[e.path]
            remember.forget('coffeeRem', e.path)
    gulp.watch config.paths.assets, ['assets']
    gulp.watch config.paths.index, ['index']

gulp.task('serve', ['watchReal'] , ->
    gulp.src('www')
        .pipe(webserver({
            host: '192.168.1.6',
            port:'8100',
            livereload: true,
            open: true
        }));
)